﻿import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vischat/data/models/user_model.dart';
import 'package:vischat/data/repositories/auth_repository.dart';
import 'package:vischat/generated/route_table.dart';

class UserController extends GetxController
    with GetSingleTickerProviderStateMixin {
  final AuthRepository _repo;
  UserController(this._repo);

  late TabController _tabController;
  final List<String> _tabs = [
    'page.user.tab.posts'.tr,
    'page.user.tab.collect'.tr,
  ];

  final _userInfo = Rx<UserModel?>(null);

  UserModel? get userInfo => _userInfo.value;
  int? get _userId => Get.arguments?['userId'];
  bool get isSelf => _userId == null;
  List<String> get tabs => _tabs;
  TabController get tabController => _tabController;

  @override
  void onInit() {
    super.onInit();
    _initTab();
    _getUserInfo();
  }

  void _initTab() {
    if (isSelf) {
      _tabs.add('page.user.tab.message'.tr);
    }
    _tabController = TabController(length: _tabs.length, vsync: this);
  }

  Future _getUserInfo() async {
    final user = await _repo.fetchUser(userId: _userId, isSelf: isSelf);
    _userInfo.value = user;
  }

  void handleMenu() {
    // TODO 菜单处理
  }

  void handleFollow() {
    // TODO 关注/取关
  }

  void goUserDetail() {
    Get.toNamed(RouteTable.USER_EDIT, arguments: {'userInfo': _userInfo.value});
  }

  void goAuthenticate() {
    // TODO 去认证
  }

  void goChat() {
    // TODO 发私信
  }

  void goVisitor() {
    // TODO 查看访客
  }

  @override
  void onClose() {
    _tabController.dispose();
    super.onClose();
  }
}
